package name.marz.to.go.ahg.SMBC.executer;

import java.io.IOException;
import java.util.logging.Logger;

import name.marz.to.go.ahg.core.AutoGetterHttpClient;
import name.marz.to.go.ahg.core.exception.OutOfServiceException;
import name.marz.to.go.ahg.core.executer.DocumentGetExecuter;
import name.marz.to.go.ahg.core.responsehandler.DocumentResponseHandler;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.jsoup.nodes.Document;

public class SMBCTopPageExecuter extends DocumentGetExecuter {
	private static final Logger log = Logger.getLogger(SMBCTopPageExecuter.class.getName());

	private static final String SYSTEM_MAINTAIN_STR="システムメンテナンス中";
	
	/**
	 * @param host
	 * @param path
	 * @param client
	 */
	public SMBCTopPageExecuter(String host, String path, AutoGetterHttpClient client) {
		super(host+path, client);
	}

	@Override
	public Document execute() throws ClientProtocolException, IOException {
		// リクエスト生成
		HttpUriRequest req = new HttpGet(uri);
		// レスポンスハンドラ生成
		DocumentResponseHandler handler = new DocumentResponseHandler(client.getDefaultCharset());
		//　リクエスト送信
		log.info("トップページ処理中 " + req.getURI());
		Document doc = client.execute(req, handler);
		// システムメンテナンス中か判定
		if ( doc.html().contains(SYSTEM_MAINTAIN_STR) ) throw new OutOfServiceException();
		return doc;
	}

}
